/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    light_sensor.c
  * @brief   This file provides code for the configuration
  *          of the Light Sensor instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "light_sensor.h"

/* USER CODE BEGIN 0 */

/* Private variables ---------------------------------------------------------*/
static LightSensor_ConfigTypeDef light_sensor_config = {
    .threshold = LIGHT_SENSOR_DEFAULT_THRESHOLD,
    .auto_led_control = 1,
    .invert_logic = 0
};

static uint8_t light_sensor_initialized = 0;

/* USER CODE END 0 */

/* USER CODE BEGIN 1 */

/**
  * @brief  初始化光敏传感器模块
  * @param  config: 配置参数指针，如果为NULL则使用默认配置
  * @retval LightSensor_StatusTypeDef
  */
LightSensor_StatusTypeDef LightSensor_Init(LightSensor_ConfigTypeDef *config)
{
    // 如果提供了配置参数，则使用用户配置
    if (config != NULL) {
        light_sensor_config = *config;
    }
    
    // 标记为已初始化
    light_sensor_initialized = 1;
    
    return LIGHT_SENSOR_OK;
}

/**
  * @brief  读取光敏传感器原始ADC值
  * @param  value: 存储读取值的指针
  * @retval LightSensor_StatusTypeDef
  */
LightSensor_StatusTypeDef LightSensor_ReadRaw(uint16_t *value)
{
    if (!light_sensor_initialized || value == NULL) {
        return LIGHT_SENSOR_ERROR;
    }
    
    // 启动ADC转换
    if (HAL_ADC_Start(LIGHT_SENSOR_ADC_HANDLE) != HAL_OK) {
        return LIGHT_SENSOR_ERROR;
    }
    
    // 等待转换完成
    if (HAL_ADC_PollForConversion(LIGHT_SENSOR_ADC_HANDLE, LIGHT_SENSOR_ADC_TIMEOUT) != HAL_OK) {
        HAL_ADC_Stop(LIGHT_SENSOR_ADC_HANDLE);
        return LIGHT_SENSOR_TIMEOUT;
    }
    
    // 获取ADC值
    *value = HAL_ADC_GetValue(LIGHT_SENSOR_ADC_HANDLE);
    
    // 停止ADC转换
    HAL_ADC_Stop(LIGHT_SENSOR_ADC_HANDLE);
    
    return LIGHT_SENSOR_OK;
}

/**
  * @brief  获取当前光照状态
  * @retval LightSensor_StateTypeDef
  */
LightSensor_StateTypeDef LightSensor_GetState(void)
{
    uint16_t light_value = 0;
    
    if (LightSensor_ReadRaw(&light_value) != LIGHT_SENSOR_OK) {
        return LIGHT_STATE_BRIGHT; // 默认返回明亮状态
    }
    
    // 根据阈值判断光照状态
    if (light_value < light_sensor_config.threshold) {
        return LIGHT_STATE_DARK;
    } else {
        return LIGHT_STATE_BRIGHT;
    }
}

/**
  * @brief  设置光照阈值
  * @param  threshold: 新的阈值
  * @retval None
  */
void LightSensor_SetThreshold(uint16_t threshold)
{
    light_sensor_config.threshold = threshold;
}

/**
  * @brief  获取当前光照阈值
  * @retval uint16_t: 当前阈值
  */
uint16_t LightSensor_GetThreshold(void)
{
    return light_sensor_config.threshold;
}

/**
  * @brief  LED控制函数
  * @param  state: 光照状态
  * @retval None
  */
void LightSensor_LED_Control(LightSensor_StateTypeDef state)
{
    GPIO_PinState led_state;
    
    // 根据逻辑反转设置决定LED状态
    if (light_sensor_config.invert_logic) {
        // 反转逻辑：亮时点亮LED
        led_state = (state == LIGHT_STATE_BRIGHT) ? GPIO_PIN_SET : GPIO_PIN_RESET;
    } else {
        // 正常逻辑：暗时点亮LED
        led_state = (state == LIGHT_STATE_DARK) ? GPIO_PIN_SET : GPIO_PIN_RESET;
    }
    
    HAL_GPIO_WritePin(LIGHT_SENSOR_LED_PORT, LIGHT_SENSOR_LED_PIN, led_state);
}

/**
  * @brief  自动LED控制（根据当前光照状态自动控制LED）
  * @retval None
  */
void LightSensor_AutoLEDControl(void)
{
    if (!light_sensor_initialized || !light_sensor_config.auto_led_control) {
        return;
    }
    
    LightSensor_StateTypeDef current_state = LightSensor_GetState();
    LightSensor_LED_Control(current_state);
}

/**
  * @brief  使能/禁用自动LED控制
  * @param  enable: 1-使能，0-禁用
  * @retval None
  */
void LightSensor_SetAutoLEDControl(uint8_t enable)
{
    light_sensor_config.auto_led_control = enable ? 1 : 0;
}

/**
  * @brief  设置逻辑反转
  * @param  invert: 1-反转逻辑，0-正常逻辑
  * @retval None
  */
void LightSensor_SetInvertLogic(uint8_t invert)
{
    light_sensor_config.invert_logic = invert ? 1 : 0;
}

/* USER CODE END 1 */